//
//  LCBaseDatabase+DBAlter.m
//  LCFMDB
//
//  Created by jiukuyinyue on 2017/10/31.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCBaseDatabase+DBAlter.h"

@implementation LCBaseDatabase (DBAlter)
- (void)alterDbWithSqliteName:(NSString *)sqliteName
                        sqlStr:(NSString *)sql
                        column:(NSString *)column
               tableName:(NSString *)tableName
                     succBlock:(void(^)(void))succBlock
                     failBlock:(void(^)(void))failBlock {
    
    [self.db inDatabase:^(FMDatabase *db) {
        BOOL isOK = [db columnExists:column inTableWithName:tableName];
        if (!isOK) {
            BOOL isAlter = [db executeUpdate:sql];
            if (isAlter) {
                succBlock();
            }else{
                JKLog(@"[db lastErrorMessage] = %@", [db lastErrorMessage]);;//打印错误日志
                failBlock();
            }
        }
        
    }];
}
#pragma mark - EssayParametersListTable
- (void)alterIDEssayParametersListTable {
    
    NSString *sqliteName = kDbSqlName;//库
    NSString *sql = ALTER_ESSAYTABLE_CID;
    
    [self alterDbWithSqliteName:sqliteName
                          sqlStr:sql
                         column:@"cid"
                      tableName:@"EssayTable"
                      succBlock:^{
//        NSLog(@"改变 table 'EssayInfoTable' 成功. 成功增加cid字段");
    } failBlock:^{
        NSLog(@"Error:改变 EssayInfoTable 失败");
    }];
}

#pragma mark - EssayParametersListTable
- (void)alterSrcEssayParametersListTable {
    
    NSString *sqliteName = kDbSqlName;//库
    NSString *sql = ALTER_ESSAYTABLE_SRC;
    
    [self alterDbWithSqliteName:sqliteName
                         sqlStr:sql
                         column:@"src"
                      tableName:@"EssayTable"
                      succBlock:^{
                          //        NSLog(@"改变 table 'EssayInfoTable' 成功. 成功增加src字段");
                      } failBlock:^{
                          NSLog(@"Error:改变 EssayInfoTable 失败");
                      }];
}
@end
